/*
 * Copyright 2012 Marsrutai.lt
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package lt.marsrutai.mobile.android.fragment;

import java.util.Arrays;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

import lt.marsrutai.mobile.android.activity.intent.FastestRoutesSearchIntentHolder;
import lt.marsrutai.mobile.android.widget.DialogChoiceItem;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;

/**
 * Dialog which presents the user with a list of options which ultimately are
 * but textual representations of various points on a map
 * 
 * @author Justas
 */
public class PointSelectionDialogFragment extends DialogFragment {
    
    /** object receiving event callbacks */
    private PointSelectionDialogListener listener;

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        FastestRoutesSearchIntentHolder intentHolder = new FastestRoutesSearchIntentHolder(getArguments());

        final String ownerId = intentHolder.getDialogOwnerId();
        final List<DialogChoiceItem> listItems = new LinkedList<DialogChoiceItem>(Arrays.asList(intentHolder.getDialogChoices()));
        List<String> listItemNames = new LinkedList<String>();

        /*
         * Remove options which are marked as "single use" and are already
         * selected from a different dialog which shares the same option pool
         */
        Iterator<DialogChoiceItem> iterator = listItems.iterator();
        while (iterator.hasNext()) {
            DialogChoiceItem item = iterator.next();
            if (item.isUsable() || listener.isSelected(ownerId, item)) {
                listItemNames.add(getString(item.getNameResourceId()));
            }
            else {
                iterator.remove();
            }
        }

        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        builder.setTitle(intentHolder.getDialogTitle()).setItems(listItemNames.toArray(new String[listItemNames.size()]),
                new DialogInterface.OnClickListener() {

                    public void onClick(DialogInterface dialog, int which) {
                        listener.onDialogClick(ownerId, listItems.get(which));
                    }
                });

        return builder.create();
    }
    
    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        try {
            // Register parent activity as a callback listener
            this.listener = (PointSelectionDialogListener) activity;
        } catch (ClassCastException e) {
            throw new ClassCastException(activity.toString() + " must implement " + PointSelectionDialogListener.class);
        }
    }

    /**
     * Interface for receiving event callbacks
     * 
     * @author Justas
     */
    public interface PointSelectionDialogListener {
        
        /**
         * Called when the user performs a click on the dialog
         * 
         * @param ownerId 
         * @param item The selected {@link DialogChoiceItem}  
         */
        public void onDialogClick(String ownerId, DialogChoiceItem item);

        /**
         * Checks whether the owner of this dialog already has the item selected
         * 
         * @param ownerId
         * @param item
         * @return
         */
        public boolean isSelected(String ownerId, DialogChoiceItem item);
    }
}
